package a.a.a.a.a;

import com.a.a.b;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;

public class a {
    private static final Charset a = new b().charsetForName("X-MODIFIED-UTF-7");

    public static String a(String str) {
        return new String(a(str, a), Charset.forName("US-ASCII"));
    }

    private static ByteBuffer a(ByteBuffer byteBuffer) {
        if (byteBuffer.capacity() == 0) {
            return ByteBuffer.allocate(1);
        }
        ByteBuffer allocate = ByteBuffer.allocate(byteBuffer.capacity() * 2);
        byteBuffer.flip();
        allocate.put(byteBuffer);
        return allocate;
    }

    private static ByteBuffer a(CharBuffer charBuffer, CharsetEncoder charsetEncoder) {
        ByteBuffer allocate = ByteBuffer.allocate((int) (((double) charBuffer.remaining()) * ((double) charsetEncoder.averageBytesPerChar())));
        charsetEncoder.reset();
        ByteBuffer byteBuffer = allocate;
        CoderResult coderResult = null;
        while (coderResult != CoderResult.UNDERFLOW) {
            if (charsetEncoder.encode(charBuffer, byteBuffer, true) == CoderResult.OVERFLOW) {
                byteBuffer = a(byteBuffer);
            } else {
                coderResult = charsetEncoder.flush(byteBuffer);
                if (coderResult == CoderResult.OVERFLOW) {
                    byteBuffer = a(byteBuffer);
                }
            }
        }
        byteBuffer.flip();
        return byteBuffer;
    }

    public static byte[] a(String str, Charset charset) {
        CharBuffer wrap = CharBuffer.wrap(str.toCharArray());
        ByteBuffer a = a(wrap.asReadOnlyBuffer(), charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE));
        byte[] bArr = new byte[a.limit()];
        a.get(bArr);
        return bArr;
    }
}
